# Files Structure:
# ------------------------------------------------------------
# include/
# obj/[debug|release]
# src/
# Makefile

DIR_BIN:=bin
DIR_SRC:=src
DIR_OBJ:=obj
DIR_INC:=include

EXT_SRC:=c
EXT_INC:=h

BIN:=ChessFive
INC:=$(wildcard $(DIR_INC)/*.$(EXT_INC))
SRC:=$(wildcard $(DIR_SRC)/*.$(EXT_SRC))
RES:=

CC:=clang
CFLAGS:=-Wall -std=c11 -DPLATFORM_DESKTOP
CFLAGS+=-I./$(DIR_INC)
# link static lib: -Wl,-Bstatic -l<static>, or /<dir>/lib.a
# link dynamic lib: -Wl,-Bdynamic -l<dynamic>
# system libs required -Wl,-Bdynamic
# e.g. LFLAGS:=-Wl,-Bstatic -l<lib> -Wl,-Bdynamic
LFLAGS:=-lraylib

ifeq ($(OS),Windows_NT)		# windows
	RES+=$(DIR_OBJ)/resource.res
	LFLAGS+=
else
	UNAME=$(shell uname -s)
	ifeq ($(UNAME),Darwin)	# macos
		LFLAGS+=
	else					# linux, ...
		LFLAGS+=
	endif
endif

.PONY: debug release
# debug
debug: CFLAGS+=-D_DEBUG -g
debug: $(shell mkdir -p $(DIR_OBJ)/debug)
debug: $(SRC:$(DIR_SRC)/%.$(EXT_SRC)=$(DIR_OBJ)/debug/%.o) $(RES)
	$(CC) -o $(DIR_BIN)/$(BIN)_d $^ $(LFLAGS)
# release
release: CFLAGS+=-DNDEBUG -O3
release: $(shell mkdir -p $(DIR_OBJ)/release)
release: $(SRC:$(DIR_SRC)/%.$(EXT_SRC)=$(DIR_OBJ)/release/%.o) $(RES)
	$(CC) -o $(DIR_BIN)/$(BIN) $^ $(LFLAGS)

$(DIR_OBJ)/debug/%.o $(DIR_OBJ)/release/%.o: $(DIR_SRC)/%.$(EXT_SRC) $(INC)
	$(CC) $(CFLAGS) -c $< -o $@

$(DIR_OBJ)/%.res: $(DIR_SRC)/%.rc
	windres -J rc -O coff -i $< -o $@

.PONY: cleand cleanr
# clean debug
cleand:
	rm -f $(DIR_OBJ)/debug/*.o
	rm -f $(DIR_BIN)/$(BIN)_d
# clean release
cleanr:
	rm -f $(DIR_OBJ)/release/*.o
	rm -f $(DIR_BIN)/$(BIN)